<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            async function main(){
                //await 表达式
                //1. new 实例化
                // let result = await new Promise((resolve, reject) => {
                //     // resolve('OK');
                //     resolve([]);
                // });

                //2. Promise.resolve
                // let result = await Promise.resolve('醒醒');

                //3. 失败的情况
                try{
                    // let result = await Promise.reject('失败啦');
                    let result = await Promise.resolve('成功啦');
                    //这里相当于是 then 方法中成功回调的位置
                    console.log(result);
                }catch(e){
                    //这里相当于是 then 方法中失败回调的位置
                    console.log(e); //这里错误的结果就是 promise 失败的结果值
                }

                //4. 其他值 (情况很少见)
                // let result = await 20;
            }

            //调用函数
            // main();


            async function fn(){
                //await 表达式  『等待』
                let result = await new Promise((resolve, reject) => {
                    setTimeout(() => {
                        resolve('OK');
                    }, 2000)
                });

                //查看结果
                console.log(result);
            } 

            fn();




        </script>
    </body>
</html>
